package com.z.controller;

import com.z.pojo.Goods;
import com.z.pojo.PageBean;
import com.z.service.GoodsService;
import com.z.service.impl.GoodsServiceImpl;
import com.z.utils.TermUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

@WebServlet("/goods")
public class GoodsTypeServlet extends HttpServlet {
    private final GoodsService gsi = new GoodsServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String tid = req.getParameter("tid");
        String nowpage = req.getParameter("nowpage");
        String eachcount = req.getParameter("eachcount");

        String ordurl = String.valueOf(req.getRequestURL());
        String url = ordurl+"?"+(req.getQueryString()==null?"":req.getQueryString());
        if(url.contains("&nowpage")){
            url = url.substring(0,url.indexOf("&nowpage"));
        }
        if(nowpage == null||"".equals(nowpage)){
            nowpage ="1";
        }
        if(eachcount == null||"".equals(eachcount)){
            eachcount ="8";
        }
        HashMap<String, String[]> map = new HashMap<>();
        map.put("tid",new String[]{tid});

        String term = TermUtil.getTerm(map);

        PageBean<Goods> pageBean = gsi.goodsFormType(Integer.valueOf(nowpage), Integer.valueOf(eachcount), term);

        pageBean.setUrl(url);
        System.out.println("pageBean = " + pageBean);
        req.setAttribute("ipage",pageBean);
        req.getRequestDispatcher("/product.jsp").forward(req,resp);

    }
}
